<template>
  <div class="search-content" ref="container">
    <div class="title-wrap">
      <h2 class="title">{{ $route.query.keywords }}</h2>
      <span class="sub-title">找到{{ total }}个结果</span>
    </div>
    <el-tabs v-model="type">
      <el-tab-pane label="歌曲" name="1">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              v-for="(item, index) in songList"
              :key="item.id"
              @dblclick="rowDbclick(item.id)"
            >
              <td>{{ index + 1 }}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span class="name">{{ item.name }}</span>
                    <span
                      v-if="item.mvid != 0"
                      class="iconfont icon-mv"
                      @click="toMV(item.mvid)"
                    ></span>
                  </div>
                  <span class="sub-name" v-if="item.alias.length != 0">{{
                    item.alias[0]
                  }}</span>
                </div>
              </td>
              <td>{{ item.artists[0].name }}</td>
              <td>{{ item.album.name }}</td>
              <td>{{ item.duration | formatDuration }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="1000">
        <div class="items">
          <div
            class="item"
            v-for="item in playList"
            :key="item.id"
            @click="toPlaylist(item.id)"
          >
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{ item.playCount | formatCount }}</span>
              </div>
              <img :src="item.coverImgUrl" alt="" />
              <span class="iconfont icon-play el-icon-video-play"></span>
            </div>
            <p class="name">{{ item.name }}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="1004">
        <div class="items mv">
          <div
            class="item"
            v-for="item in mvList"
            :key="item.id"
            @click="toMV(item.id)"
          >
            <div class="img-wrap">
              <img :src="item.cover" alt="" />
              <span class="iconfont icon-play el-icon-video-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{ item.playCount | formatCount }}</div>
              </div>
              <span class="time">{{ item.duration | formatDuration }}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{ item.name }}</div>
              <div class="singer">{{ item.artistName }}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="limit"
    >
    </el-pagination>
  </div>
</template>

<script>
import { search } from '@/api/search'
import { songUrl, Lyric } from '@/api/home'
import bus from '@/utils/bus'
export default {
  name: 'SearchIndex',
  components: {},
  props: {},
  data () {
    return {
      type: '1',
      // 页容量
      limit: 80,
      // 页码
      page: 1,
      // 总条数
      total: 0,
      songList: [],
      playList: [],
      mvList: [],
      searchText: ''
    }
  },
  computed: {},
  watch: {
    searchText () {
      this.searchResult()
    },
    type: [
      function () {
        this.limit = this.type === '1004' ? 80 : 60
      },
      'searchResult'
    ],
    '$route.query.keywords': 'searchResult'
  },
  created () {
    this.OnBus()
    this.searchText = this.$route.params.keywords
    this.searchResult()
  },
  mounted () {},
  methods: {
    OnBus () {
      bus.$on('searchText', (data) => {
        this.searchText = data
      })
    },
    toPlaylist (id) {
      this.$router.push({
        name: 'playlist',
        params: {
          id
        }
      })
    },
    // 双击某一行
    async rowDbclick (id) {
      const lyric = await Lyric(id)
      const res = await songUrl(id)
      bus.$emit('songurl', res.data.data[0].url, lyric.data.lrc.lyric)
    },
    // 去mv页面
    toMV (id) {
      this.$router.push({
        name: 'mv',
        params: {
          id
        }
      })
    },
    // 页码改变
    handleCurrentChange (val) {
      this.page = val
      this.searchResult()
    },
    async searchResult () {
      const { limit, type, page } = this
      // const { keywords } = this.$route.params

      const res = await search(
        this.searchText,
        limit,
        (page - 1) * limit,
        type
      )
      // 根据类型不同
      switch (this.type) {
        case '1':
          this.songList = res.data.result.songs
          this.total = res.data.result.songCount
          break
        case '1000':
          this.playList = res.data.result.playlists
          this.total = res.data.result.playlistCount
          break

        default:
          this.mvList = res.data.result.mvs
          this.total = res.data.result.mvCount
          break
      }
    }
  }
}
</script>

<style scoped lang="less">
.search-content{
  .title-wrap {
    display: flex;
    align-items: flex-end;
    margin-bottom: 30px;
    .title {
      margin-right: 10px;
    }
    .sub-title {
      font-size: 15px;
      color: #bebebe;
    }
  }
  .el-tabs__item {
    font-size: 18px;
  }
  .el-table td,
  .el-table th.is-leaf {
    border-bottom: none;
  }
  .items {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    .item {
      width: 200px;
      cursor: pointer;
      margin-right: 20px;
      margin-bottom: 20px;
      .img-wrap {
        width: 100%;
        position: relative;
        > .icon-play {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 40px;
          height: 40px;
          color: #dd6d60;
          font-size: 20px;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          background: rgba(255, 255, 255, 0.8);
          opacity: 0;
        }
        &:hover > .icon-play {
          opacity: 1;
        }
        img {
          width: 100%;
          border-radius: 5px;
        }
        .num-wrap {
          position: absolute;
          color: white;
          top: 0;
          right: 0;
          display: flex;
          align-items: center;
          font-size: 15px;
          padding-right: 5px;
          padding-top: 2px;
          .icon-play {
            font-size: 12px;
            display: flex;
            align-items: center;
            margin-right: 5px;
          }
        }
        .time {
          position: absolute;
          bottom: 5px;
          right: 5px;
          color: white;
          font-size: 15px;
        }
      }
      .name {
        font-size: 15px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
      .singer {
        font-size: 14px;
        color: #c5c5c5;
      }
    }
    &.mv {
      .item {
        width: 250px;
      }
    }
  }
}
.el-table th, .el-table tr {
  background-color: #e9eef3;
}
.el-table, .el-table__expanded-cell{
  background-color: #e9eef3;
}
</style>
